package com.wlb.test.service.rabbitmq.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.wlb.test.util.rabbitmq.ConnectionUtil;
import com.wlb.test.util.rabbitmq.QueueEnum;


/**
 * @Author:wanglibing
 * @Mail:wanglibing@jd.com
 * @Date:Created in 21:08 2018/1/29
 * @Description:
 */
public class Recv {
    public static void main(String[] args) throws Exception{
        Connection connection = ConnectionUtil.getConnection();
//        客户端与队列或者交换机的通道
        Channel channel = connection.createChannel();
//        声明队列
        channel.queueDeclare(QueueEnum.QUEUE_NAME_SIMPLE.getName(),false,false,false,null);
//        定义队列的消费者
        QueueingConsumer consumer = new QueueingConsumer(channel);
//        监听队列
        channel.basicConsume(QueueEnum.QUEUE_NAME_SIMPLE.getName(),true,consumer);
//        不停的获取消息
        while(true){
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [X]  Recieved '" + message + "'");
        }
    }
}
